﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.Orders
{
    public partial class MyOrders : BasePage
    {
        private static IOrdersBLL ordersBLL = IocContainer.Instance.Resolve<IOrdersBLL>();
        protected int CustId = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                CustId = IndividualId;
                this.txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                this.btnDelete.Enabled = false;
                this.btnUpdate.Enabled = false;
                this.btnSubmit.Enabled = false;
            }
        }

        [WebMethod]
        public static Boolean DeleteOrder(int orderId)
        {

            Boolean result = false;
            try
            {
                ordersBLL.Delete(orderId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        [WebMethod]
        public static Boolean SubmitOrder(int orderId, int status)
        {
            Boolean result = false;
            try
            {
                ordersBLL.Submit(orderId, status);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }


        [WebMethod]
        public static string GetOrders(int custId,int userType, string status, string orderCode, string startDate, string endDate, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");

            if (userType ==2 && custId>0)//客户
            {
                strWhere.Append(" and T_Order.FCustomersID=" + custId);
            }
            else if(userType==1 && custId>0)
            {
                strWhere.Append(" and T_Order.Fupdater=" + custId);
            }

            if (!string.IsNullOrEmpty(status) && status!="-1")
            {
                strWhere.Append(" and T_Order.Fstatus =" + status);
            }
            if (!string.IsNullOrEmpty(orderCode))
            {
                strWhere.Append(" and T_Order.Fnumber like '" + orderCode + "%'");
            }
            if (!string.IsNullOrEmpty(startDate) && string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate >='" + orderCode + "'");
            }
            else if (string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate <='" + endDate.Trim() + " 23:59:59'");
            }
            else if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate between '" + startDate + "' and '" + endDate.Trim() + " 23:59:59'");
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FUpdateDate  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  desc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = ordersBLL.GetOrdersByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            string strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";

        }

    }
}
